<?php

defined('IN_FLYWHEEL') or die();

/**
 * Flywheel Validator
 * 	abstract base class for all validator
 * 
 * @package 	system
 * @subpackage 	validator
 * @version 	$Id$
 * @author 		Luu Trong Hieu <tronghieu1012@yahoo.com>
 *
 */
abstract class fValidator {
	/**
	 * @var array
	 */
	protected $_options = array();
	protected $_messages = array();
	protected $_failuresOptions = array();
	
	public function __construct($options) {
		array_merge($this->_options, $options);		
	}
	
	abstract public function validate($value);
	
	/**
	 * Set option
	 *
	 * @param string $key
	 * @param mixed $value
	 */
	public function setOption($key, $value) {
		$this->_options[$key] = $value;		
	}
	
	/**
	 * Get Options
	 *
	 * @param string $key. If key = null, return all option
	 * @return value of option
	 */
	public function getOptions($key = null) {
		if ($key == null) {
			return $this->_options;
		}
		return $this->_options[$key];
	}
	
	/**
	 * Clear Options
	 *
	 */
	public function clearOptions() {
		$this->_options = array();
	}
	
	/**
	 * Add message for option
	 *
	 * @param string $option. Option name
	 * @param string $mess
	 */
	public function addMessage($option, $mess) {
		$this->_messages[$option] = $mess;		
	}
	
	/**
	 * Add Messages by array
	 *
	 * @param array $messages
	 */
	public function addMessages($messages) {
		if (is_array($messages)) {
			array_merge($this->_messages, $messages);
		}
	}
	
	/**
	 * Get Message by option
	 *
	 * @param mixed string|null $option. If $option = null mean that return all option
	 * @return value of message
	 */
	public function getMessage($option = null) {
		if ($option == null) {
			return $this->_messages;		
		}
		
		return $this->_messages[$option];
	}
	
	/**
	 * Get failures
	 *
	 * @return array
	 */
	public function getFailures() {
		return $this->_failuresOptions;
	}
	
	/**
	 * Get Failures Messages
	 *
	 * @return array
	 */
	public function getFailuresMessage() {
		$mess = array();
		if (($size = sizeof($this->_failuresOptions)) === 0) {
			return $mess;		
		}
		
		for ($i = 0; $i < $size; ++$i) {
			$mess[] = $this->_messages[$this->_failuresOptions[$i]];
		}
		
		return $mess;
	}
}